package com.suyuan.controller.shop;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.OrderInfo;
import com.suyuan.entity.OrderitemInfo;
import com.suyuan.params.MyOrderParam;
import com.suyuan.params.SaveOrderParam;
import com.suyuan.service.OrderService;
import com.suyuan.utils.JsonMsg;
import com.suyuan.utils.JsonUtils;
import com.suyuan.vo.OrderDetailVO;
import com.suyuan.vo.OrderInfoVO;
import com.suyuan.web.Page;
import com.suyuan.web.PageData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单控制器
 */
@Controller
@RequestMapping(value = "/shop/order")
public class OrderController {
    @Resource(name = "orderService")
    private OrderService orderService;

    /**
     * 保存订单
     */
    @RequestMapping(value = "/saveOrder")
    @ResponseBody
    public Object saveOrder(@RequestBody String data){
        SaveOrderParam param = JsonUtils.jsonToPojo(data, SaveOrderParam.class);

        System.out.println(param.getItems());
        System.out.println(param.getUserId());
        System.out.println(param.getAddrId());

        if(param.getItems() == null || param.getUserId() == null || param.getAddrId() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        boolean isSuccess = false;
        isSuccess = orderService.saveOrder(param.getItems(), param.getUserId(), param.getAddrId());
        if(isSuccess){
            return JsonMsg.SuccessMsg(null);
        }
        return JsonMsg.ErrorMsg("订单创建失败");
    }

    /**
     * 我的订单
     * 已测试
     */
    @RequestMapping(value = "/myOrders")
    @ResponseBody
    public Object myOrders(@RequestBody String data){
        List<OrderInfoVO> orderInfoVOS = null;
        try {
            MyOrderParam param = JsonUtils.jsonToPojo(data, MyOrderParam.class);
            if(param.getId() == null){
                return JsonMsg.ErrorMsg("参数不能为空");
            }
            orderInfoVOS = orderService.myOrders(param.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonMsg.ErrorMsg("错误错误");
        }
        return JsonMsg.SuccessMsg(orderInfoVOS);
    }

    /**
     * 订单详情
     * 已测试
     */
    @RequestMapping(value = "/orderDetail")
    @ResponseBody
    public Object orderDetail(@RequestBody String data){
        OrderInfo order = JsonUtils.jsonToPojo(data, OrderInfo.class);
        if(order.getGid() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        OrderDetailVO orderDetailVO = orderService.orderDetail(order.getGid());
        if(orderDetailVO == null){
            return JsonMsg.ErrorMsg("订单不存在");
        }
        return JsonMsg.SuccessMsg(orderDetailVO);
    }
}
